package com.instagram.instadownloader.history;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.instagram.instadownloader.InstaDownloaderApp;

/**
 * The Class HistoryManager.
 * @author HuanND
 */
public class HistoryManager {
	
	/** The instance. */
	private static HistoryManager INSTANCE;
	
	/** The Constant FILE_NAME. */
	private static final String FILE_NAME = "_instaDownloderHistory";
	
	/** The Constant PREF_KEY. */
	private static final String PREF_KEY = "_instadownloaderkey";
	
	/** The m list history object. */
	private ArrayList<HistoryObject> mListHistoryObject;
	
	/** The m prefs. */
	private SharedPreferences  mPrefs;
	
	/**
	 * Instantiates a new history manager.
	 */
	private HistoryManager(){
		mPrefs = InstaDownloaderApp.getAppContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		retrieveData();
	}
	
	/**
	 * Gets the single instance of HistoryManager.
	 *
	 * @return single instance of HistoryManager
	 */
	public static HistoryManager getInstance(){
		if(INSTANCE==null){
			INSTANCE = new HistoryManager();
		}
		
		return INSTANCE;
	}
	
	/**
	 * Adds the history.
	 *
	 * @param object the object
	 */
	public void addHistory(HistoryObject object){
		 mListHistoryObject.add(object);
		 saveData();
	}
	
	/**
	 * Removes the from history.
	 *
	 * @param object the object
	 */
	public void removeFromHistory(HistoryObject object){
		if(mListHistoryObject.contains(object)){
			mListHistoryObject.remove(object);
			saveData();
		}
	}
	
	/**
	 * Removes the from history.
	 *
	 * @param absolutePath the absolute path
	 */
	public void removeFromHistory(String absolutePath){
		for(int i=0;i<mListHistoryObject.size();i++){
			HistoryObject object = mListHistoryObject.get(i);
			String path = object.getAbsolutePath();
			boolean bool = path.equals(absolutePath);
			if(bool){
				removeFromHistory(object);
				return;
			}
		}
	}
	
	public void removeFromHistory(int pos){
		if(mListHistoryObject.size()>pos){
			mListHistoryObject.remove(pos);
			saveData();
		}
	}
	
	/**
	 * Gets the history list.
	 *
	 * @return the history list
	 */
	public ArrayList<HistoryObject> getHistoryList(){
		return mListHistoryObject;
	}
	
	/**
	 * Save data.
	 */
	private void saveData(){
		 Editor prefsEditor = mPrefs.edit();
	     Gson gson = new Gson();
	     String json = gson.toJson(mListHistoryObject);
	     clearData();
	     prefsEditor.putString(PREF_KEY, json);
	     prefsEditor.commit();
	}
	
	/**
	 * Clear data.
	 */
	private void clearData(){
		 Editor prefsEditor = mPrefs.edit();
		 prefsEditor.remove(PREF_KEY);
	}
	
	/**
	 * Retrieve data.
	 */
	private void retrieveData(){
		Gson gson = new Gson();
	    String json = mPrefs.getString(PREF_KEY, "");
	    Type type = new TypeToken<List<HistoryObject>>(){}.getType();
	    mListHistoryObject = gson.fromJson(json, type);
	    if(mListHistoryObject==null){
	    	mListHistoryObject = new ArrayList<HistoryObject>();
	    }
	}
}
